<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */

class b2c_goods_supplement {

    public function __construct($app){
        $this->app = $app;
    }
    public function notify($product_id){
        $conf_notify = app::get('b2c')->getConf('messenger.actions.goods-notify');
        if (!$conf_notify){
            //未设置不能发送邮件短信
            return false;
        }
        $page = 1;
        $systmpl = app::get('b2c')->model('member_systmpl');
        $obj_product = app::get('b2c')->model('products');
        $member_goods = app::get('b2c')->model('member_goods');
        while ( $data = $this->getListNeedSto($member_goods ,$page,$product_id)) {
            foreach($data as $sto_product ){
                //通知
                self::send($conf_notify ,$systmpl ,$obj_product,$sto_product);
            }
            $page++;
        }
    }
    /**
     * 获取需要通知的列表
     * Time：2016/11/29 20:16:29
     * @author li
     * @param page int 页数
     * @return array
    */
    public function getListNeedSto($model ,$page = 1,$product_id){
        $page_data = 50;//单页处理数量
        $offset    = ($page - 1) * $page_data;
        $limit     = $page_data;
        $time      = time();
        //开始查找数据
        $filter['product_id'] = $product_id;
        // $filter['type']        = 'supplement';
        $filter['type|in']        = array('supplement','sto');
        $filter['object_type'] = 'goods';
        $filter['status']      = 'ready';
        $filter['disabled']    = 'false';
        $filter['create_time|sthan'] = $time;
        $data = $model->getList('*',$filter,$offset,$limit);
        $data = (count($data) > 0) ? $data : false;
        return $data;
    }

    /**
     * 发送邮件
     * Time：2016/11/29 20:55:50
     * @author li
    */
    public function send($notify ,$systmpl ,$obj_product ,$sto_product){
        try{
            $arr_conf_goods_notify = explode(',',$notify);
            $goods = $obj_product->dump($sto_product['product_id']);
            if($sto_product['member_id']){
                // $member_obj = app::get('b2c')->model('members');
                $login_name = kernel::single('b2c_user_object')->get_member_name(null,$sto_product['member_id']);
            }
            else{
                $login_name = app::get('b2c')->_("顾客");
            }
            $obj['goods_name'] = $goods['name'];
            $obj['goods_id']   = $goods['goods_id'];
            $obj['username']   = $login_name;
            $obj['url'] = app::get('site')->router()->gen_url(array('app'=>'b2c','ctl'=>'site_product','full'=>1,'act'=>'index','arg'=>$sto_product['product_id']));
            $obj['url']=explode('://',$obj['url']);
            $obj['url']=explode('/',$obj['url'][1]);
            if($obj['url'][0]=='localhost'){
                $obj['url']="http://www.acfabric.com/ec_oubao/index.php/product-".$sto_product['product_id'].".html";
             }else{
                $obj['url'] = app::get('site')->router()->gen_url(array('app'=>'b2c','ctl'=>'site_product','full'=>1,'act'=>'index','arg'=>$sto_product['product_id']));
             }


            //发送邮件
            if (in_array('b2c_messenger_email',$arr_conf_goods_notify)){
                $content = $systmpl->fetch('messenger:b2c_messenger_email/goods-notify',$obj);
                $params = array(
                    'acceptor'   => $sto_product['email'],
                    'body'       => $content,
                    'title'      => app::get('b2c')->_('到货通知'),
                    'product_id' => $sto_product['product_id'],
                    'gnotify_id' => $sto_product['gnotify_id'],
                );

                system_queue::instance()->publish('b2c_tasks_sendemail', 'b2c_tasks_sendemail', $params);
            }
            //发送站内信
            if($sto_product['member_id']){
                if (in_array('b2c_messenger_msgbox',$arr_conf_goods_notify)){
                    $aTmp['content'] =  $systmpl->fetch('messenger:b2c_messenger_msgbox/goods-notify',$obj);
                    $aTmp['title'] =app::get('b2c')->_("商品到货通知");
                    $params = array(
                        'member_id'=>$sto_product['member_id'],
                        'data' =>$aTmp,
                        'name' => $login_name,
                        'gnotify_id' => $sto_product['gnotify_id'],
                    );

                    system_queue::instance()->publish('b2c_tasks_sendmsg', 'b2c_tasks_sendmsg', $params);
                }
            }
            //发到货通知到手机
            if(in_array('b2c_messenger_sms',$arr_conf_goods_notify)&&$sto_product['cellphone']){
                $aSms['content'] =  $systmpl->fetch('messenger:b2c_messenger_sms/goods-notify',$obj);
                $aSms['title'] =app::get('b2c')->_("商品到货通知");
                $aSms['sendType'] = 'fan-out';
                $params = array(
                    'gnotify_id'=>$sto_product['gnotify_id'],
                    'mobile_number'=>$sto_product['cellphone'],
                    'data' =>$aSms,
                );
                system_queue::instance()->publish('b2c_tasks_sendsms', 'b2c_tasks_sendsms', $params);
            }
        }catch(Exception $e){

        }
    }
}
